﻿using System;
using System.Text;
using System.Xml;
using Medianamik.Core.DAL;
using Medianamik.Core.DAL.SubSonicDAL;
using NUnit.Framework;
using System.Linq;
using Medianamik.Core.Sugar;

namespace Tests.Integration_Medianamik
{
    [TestFixture]
    public class NodeRevisionTests
    {
        private NodeRevision _nodeRevision;

        [TearDown]
        public void CleanUpNodeRevision()
        {
            NodeRevision.Destroy(_nodeRevision.NodeRevisionId);
        }


        [Test]
        public void StringWithSpecialCharactersShouldNotBeAlteredWhenSavedToDBThanRetreived()
        {
            const string stringWithSpecialCharacters = "⅝ Sur les tablettes d’épicerie, les choix « santé » À à Œ €";
            var nodeId = new Guid("06F96DBC-C3C9-4469-9246-002D7AB72EFF");

            _nodeRevision = new NodeRevision
                                    {
                                        Data = stringWithSpecialCharacters,
                                        NodeId = nodeId
                                    };

            _nodeRevision.Save();


            var revisions = new NodeRevisionCollection();
            revisions.Where(NodeRevision.Columns.NodeId, nodeId);
            var revision = revisions.Load().First();

            Assert.That(revision.Data, Is.EqualTo(stringWithSpecialCharacters));
        }

        [Test]
        public void NodePropertyValueLiteStringWithSpecialCharactersShouldNotBeAlteredWhenSavedToDBThanRetreived()
        {
            const string stringWithSpecialCharacters = "⅝ Sur les tablettes d’épicerie, les choix « santé » À à Œ €";

            var sanitizedXmlString = stringWithSpecialCharacters.SanitizeXmlString();

            Assert.That(sanitizedXmlString, Is.EqualTo(stringWithSpecialCharacters));

            var nodeId = new Guid("06F96DBC-C3C9-4469-9246-002D7AB72EFF");

            _nodeRevision = new NodeRevision
            {
                NodeId = nodeId
            };

            var settings = new XmlWriterSettings
            {
                Encoding = new UnicodeEncoding(false, false),
                Indent = false,
                OmitXmlDeclaration = true,
                CheckCharacters = true
            };

            using (var textWriter = new DALExtensions.StringWriterWithEncoding(Encoding.Unicode))
            {
                using (var writer = XmlWriter.Create(textWriter, settings))
                {
                    writer.WriteStartElement("root");
                    writer.WriteStartElement("properties");
                    writer.WriteStartElement("value");
                    writer.WriteCData(stringWithSpecialCharacters.SanitizeXmlString());
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                    writer.Flush();
                }

                _nodeRevision.Data = textWriter.ToString();
            }

            _nodeRevision.Save();
            var revisions = new NodeRevisionCollection();
            revisions.Where(NodeRevision.Columns.NodeId, nodeId);
            var revision = revisions.Load().First();

            Assert.That(revision.Data, Is.EqualTo(stringWithSpecialCharacters));
        }
    }
}
